<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备主键" prop="deviceid">
        <el-input
          v-model="queryParams.deviceid"
          placeholder="请输入设备主键"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编号" prop="devicenumber">
        <el-input
          v-model="queryParams.devicenumber"
          placeholder="请输入设备编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="devicename">
        <el-input
          v-model="queryParams.devicename"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备完整地址" prop="deviceaddress">
        <el-input
          v-model="queryParams.deviceaddress"
          placeholder="请输入设备完整地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="场地ID" prop="venuesid">
        <el-input
          v-model="queryParams.venuesid"
          placeholder="请输入场地ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="场地名称" prop="venuesname">
        <el-input
          v-model="queryParams.venuesname"
          placeholder="请输入场地名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="故障信息" prop="faultinfo">
        <el-input
          v-model="queryParams.faultinfo"
          placeholder="请输入故障信息"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运维人员编号" prop="replenishmentid">
        <el-input
          v-model="queryParams.replenishmentid"
          placeholder="请输入运维人员编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="故障时间" prop="ctime">
        <el-date-picker clearable size="small"
          v-model="queryParams.ctime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择故障时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="处理时间" prop="handletime">
        <el-date-picker clearable size="small"
          v-model="queryParams.handletime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择处理时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="处理状态  1：未处理  2：已处理" prop="handlestatus">
        <el-select v-model="queryParams.handlestatus" placeholder="请选择处理状态  1：未处理  2：已处理" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否删除 1.未删除 2.已删除" prop="isdel">
        <el-input
          v-model="queryParams.isdel"
          placeholder="请输入是否删除 1.未删除 2.已删除"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['equipment:fault:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['equipment:fault:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['equipment:fault:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['equipment:fault:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="faultList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备故障记录主键" align="center" prop="recordid" />
      <el-table-column label="设备主键" align="center" prop="deviceid" />
      <el-table-column label="设备编号" align="center" prop="devicenumber" />
      <el-table-column label="设备名称" align="center" prop="devicename" />
      <el-table-column label="设备完整地址" align="center" prop="deviceaddress" />
      <el-table-column label="场地ID" align="center" prop="venuesid" />
      <el-table-column label="场地名称" align="center" prop="venuesname" />
      <el-table-column label="故障信息" align="center" prop="faultinfo" />
      <el-table-column label="运维人员编号" align="center" prop="replenishmentid" />
      <el-table-column label="故障时间" align="center" prop="ctime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ctime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理时间" align="center" prop="handletime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.handletime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理状态  1：未处理  2：已处理" align="center" prop="handlestatus" />
      <el-table-column label="是否删除 1.未删除 2.已删除" align="center" prop="isdel" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['equipment:fault:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['equipment:fault:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备故障记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备主键" prop="deviceid">
          <el-input v-model="form.deviceid" placeholder="请输入设备主键" />
        </el-form-item>
        <el-form-item label="设备编号" prop="devicenumber">
          <el-input v-model="form.devicenumber" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="设备名称" prop="devicename">
          <el-input v-model="form.devicename" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备完整地址" prop="deviceaddress">
          <el-input v-model="form.deviceaddress" placeholder="请输入设备完整地址" />
        </el-form-item>
        <el-form-item label="场地ID" prop="venuesid">
          <el-input v-model="form.venuesid" placeholder="请输入场地ID" />
        </el-form-item>
        <el-form-item label="场地名称" prop="venuesname">
          <el-input v-model="form.venuesname" placeholder="请输入场地名称" />
        </el-form-item>
        <el-form-item label="故障信息" prop="faultinfo">
          <el-input v-model="form.faultinfo" placeholder="请输入故障信息" />
        </el-form-item>
        <el-form-item label="运维人员编号" prop="replenishmentid">
          <el-input v-model="form.replenishmentid" placeholder="请输入运维人员编号" />
        </el-form-item>
        <el-form-item label="故障时间" prop="ctime">
          <el-date-picker clearable size="small"
            v-model="form.ctime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择故障时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="处理时间" prop="handletime">
          <el-date-picker clearable size="small"
            v-model="form.handletime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择处理时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="处理状态  1：未处理  2：已处理">
          <el-radio-group v-model="form.handlestatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否删除 1.未删除 2.已删除" prop="isdel">
          <el-input v-model="form.isdel" placeholder="请输入是否删除 1.未删除 2.已删除" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFault, getFault, delFault, addFault, updateFault } from "@/api/equipment/fault";

export default {
  name: "Fault",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备故障记录表格数据
      faultList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceid: null,
        devicenumber: null,
        devicename: null,
        deviceaddress: null,
        venuesid: null,
        venuesname: null,
        faultinfo: null,
        replenishmentid: null,
        ctime: null,
        handletime: null,
        handlestatus: null,
        isdel: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceid: [
          { required: true, message: "设备主键不能为空", trigger: "blur" }
        ],
        devicenumber: [
          { required: true, message: "设备编号不能为空", trigger: "blur" }
        ],
        devicename: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        deviceaddress: [
          { required: true, message: "设备完整地址不能为空", trigger: "blur" }
        ],
        venuesname: [
          { required: true, message: "场地名称不能为空", trigger: "blur" }
        ],
        faultinfo: [
          { required: true, message: "故障信息不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备故障记录列表 */
    getList() {
      this.loading = true;
      listFault(this.queryParams).then(response => {
        this.faultList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordid: null,
        deviceid: null,
        devicenumber: null,
        devicename: null,
        deviceaddress: null,
        venuesid: null,
        venuesname: null,
        faultinfo: null,
        replenishmentid: null,
        ctime: null,
        handletime: null,
        handlestatus: 0,
        isdel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recordid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备故障记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordid = row.recordid || this.ids
      getFault(recordid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备故障记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.recordid != null) {
            updateFault(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFault(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordids = row.recordid || this.ids;
      this.$confirm('是否确认删除设备故障记录编号为"' + recordids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFault(recordids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('equipment/fault/export', {
        ...this.queryParams
      }, `equipment_fault.xlsx`)
    }
  }
};
</script>